package com.wt.springsamples.controller;

import com.wt.springsamples.biz.service.IOrgService;
import com.wt.springsamples.biz.service.ISysConfigService;
import com.wt.springsamples.biz.service.ISysFunctionService;
import com.wt.springsamples.common.ApiResult;
import com.wt.springsamples.model.Org;
import com.wt.springsamples.model.SysFunction;
import com.wt.springsamples.model.SysMenu;
import com.wt.springsamples.model.dto.MenuFuncConfigDto;
import com.wt.springsamples.model.dto.MenuTreeNode;
import com.wt.springsamples.model.dto.OrgTreeNode;
import com.wt.springsamples.model.dto.RoleMenuConfigDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("系统管理")
@RestController
@RequestMapping("/api/sysconfig")
public class SysConfigController {

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private ISysFunctionService sysFunctionService;

    @GetMapping("/sysmenu/{id}")
    public ApiResult getById(@PathVariable Long id){
        return ApiResult.ok();
    }

    @GetMapping("/sysmenu/listAll")
    public ApiResult listAll(){
        List<SysMenu> list = sysConfigService.listAllMenus();
        return  ApiResult.ok(list);
    }

    @PostMapping("/sysmenu/create")
    public ApiResult create(@RequestBody SysMenu sysMenu){
        sysConfigService.createMenu(sysMenu);
        return ApiResult.ok();
    }

    @PostMapping("/sysmenu/update")
    public ApiResult update(@RequestBody SysMenu sysMenu){
        if(sysMenu!=null && sysMenu.getId()!=null){
            return ApiResult.ok();
        }
        return ApiResult.fail();
    }

    @PostMapping("/sysmenu/deleteById")
    public ApiResult deleteById(long menuId){
        sysConfigService.deleteMenuById(menuId);
        return  ApiResult.ok();
    }

    @PostMapping("/sysmenu/deleteByIds")
    public ApiResult deleteByIds(@RequestBody List<Long> ids) {
        sysConfigService.deleteMenuByIds(ids);
        return  ApiResult.ok();
    }

    @GetMapping("/sysmenu/treeNode/{menuId}")
    public ApiResult treeNodeInfo(@PathVariable("menuId") long menuId){
        return ApiResult.ok();
    }

    @GetMapping("/sysmenu/treeNodeList")
    public ApiResult treeNodeList(){
        List<MenuTreeNode> treeNodeList = sysConfigService.getMenuTreeNodeList();
        return ApiResult.ok(treeNodeList);
    }

    @PostMapping("/sysmenu/apiConfig")
    public ApiResult apiConfig(MenuFuncConfigDto menuFuncConfigDto){
        sysConfigService.configMenuApi(menuFuncConfigDto);
        return ApiResult.ok();
    }

    @PostMapping("/sysmenu/addApi")
    public ApiResult addApi(Long menuId,Long funcId){
        sysConfigService.addMenuApi(menuId,funcId);
        return ApiResult.ok();
    }

    @PostMapping("/sysmenu/removeApi")
    public ApiResult removeApi(Long menuId,Long funcId){
        sysConfigService.removeMenuApi(menuId,funcId);
        return ApiResult.ok();
    }

    @PostMapping("/sysmenu/configuredApiList")
    public ApiResult configuredApiList(long menuId){
        List<SysFunction> configuredApiList = sysConfigService.listApiOfMenu(menuId);
        return ApiResult.ok(configuredApiList);
    }

    @GetMapping("/allApiList")
    public ApiResult allApiList(){
        List<SysFunction> all = sysFunctionService.getAll();
        return  ApiResult.ok(all);
    }
}
